from sqlalchemy import Column, ForeignKey, Index, Integer, String, DateTime, Date, Time, Float, Enum, Boolean
from sqlalchemy.sql import func
from sqlalchemy.orm import relation, backref

from urldb.model import *
from urldb.model.url import Url

class Trip(DeclarativeBase):
    '''
    '''
    url_record = None

    __tablename__ = 'trip'
    id = Column(Integer, primary_key=True)

    trip_planner = Column(String)
    url = Column(String)
    params = Column(String)
    doc = Column(String)
    success = Column(Boolean)
    response_time = Column(Float)
    num_itineraries = Column(Integer)

    from_lat = Column(Float)
    from_lon = Column(Float)
    from_name = Column(String)
    to_lat = Column(Float)
    to_lon = Column(Float)
    to_name = Column(String)

    message = Column(String)
    date_time = Column(DateTime)

    mode = Column(String)
    walk_distance = Column(Float)
    optimize_flag = Column(String)
    arrive_flag = Column(String)

    url_id = Column(
        Integer,
        ForeignKey(Url.id),
        nullable=False
    )
    '''
    url = relation(Url, backref=backref('urls', order_by=id))
    '''

class CompareTrip(DeclarativeBase):
    '''
    '''
    __tablename__ = 'compare_trip'
    id = Column(Integer, primary_key=True)

    def __init__(self, atrip, btrip, name):
        self.atrip_id = atrip.id
        self.btrip_id = btrip.id
        self.name = name

    name = Column(String)

    atrip_id = Column(
        Integer,
        ForeignKey(Trip.id),
        nullable=False
    )

    btrip_id = Column(
        Integer,
        ForeignKey(Trip.id),
        nullable=False
    )


class Itinerary(DeclarativeBase):
    '''
    '''
    __tablename__ = 'itinerary'
    id = Column(Integer, primary_key=True)

    best = Column(Boolean, default=False)

    start_time = Column(DateTime)
    end_time = Column(DateTime)

    wait_time = Column(Integer)
    walk_time = Column(Integer)
    transit_time = Column(Integer)
    duration = Column(Integer)
    transfers = Column(Integer)
    distance = Column(Float)

    trip_id = Column(
        Integer,
        ForeignKey(Trip.id),
        nullable=False
    )
    trip = relation(Trip, backref=backref('itineraries', order_by=id))


class TransitLeg(DeclarativeBase):
    '''Transit Leg describes the route and origin and destination points of a ride on transit vehicle 
    '''
    __tablename__ = 'transit_leg'
    id = Column(Integer, primary_key=True)

    route_name = Column(String)
    route_id = Column(String)
    stay_on_board = Column(Boolean, default=False)

    origin_name = Column(String)
    origin_id = Column(String)
    origin_lat = Column(Float)
    origin_lon = Column(Float)

    destination_name = Column(String)
    destination_id = Column(String)
    destination_lat = Column(Float)
    destination_lon = Column(Float)

    itinerary_id = Column(
        Integer,
        ForeignKey(Itinerary.id),
        nullable=False
    )
    itinerary = relation(Itinerary, backref=backref('transit_legs', order_by=id))

